package com.army.study.provider;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.app.Context;

import java.util.List;

/**
 * 公共列表项适配器
 */
public abstract class CommonItemProvider<T> extends BaseItemProvider {
    // 上下文
    protected Context mContext;
    // 列表数据
    protected List<T> listData;

    /**
     * 构造方法
     * @param mContext
     * @param listData
     */
    public CommonItemProvider(Context mContext, List<T> listData) {
        this.mContext = mContext;
        this.listData = listData;
    }

    /**
     * 获取列表数量
     * @return
     */
    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public T getItem(int i) {
        return listData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    /**
     * 获取组件对象
     * @param position
     * @param convertComponent
     * @param parent
     * @return
     */
    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer parent) {
        CommonComponentHolder holder= CommonComponentHolder.get(mContext,getXmlId(),convertComponent,parent);
        convert(holder,position,getItem(position));
        return holder.getConvertComponent();
    }

    /**
     * 子类实现
     * @return
     */
    public abstract int getXmlId();

    /**
     * 子类实现
     * @param holder
     * @param position
     * @param data
     */
    public abstract void convert(CommonComponentHolder holder, int position, T data);

}
